﻿
Imports System.Data
Imports ADOEntityLogTran
Imports ADOEntity

Partial Class Administrator_frmLogTran
    Inherits Web.UI.BasePage
    Dim LogTranEntity As New LogTransactionEntities

    Dim dsLogTran As New DataSet
    Dim clsQuery As clsDatabase
    Dim clsDataTable As New clsLINQToDataTable
    Dim intBeforeItemIndex As Integer
    Dim convertDate As clsDateFormat
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsFormatDate As clsDateFormat


#Region " Even Page "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblMsg.Text = Resources.resMessageAlert.alertEmptyGrid
        btnSearch.Text = Resources.resButton.btnSearch
        btnPrint.Text = Resources.resButton.btnPrint
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            chkIsLogin()
            If (Not IsPostBack) Then
                LoadDataToDropdown()
                CreateControlMP()
                setDefaultSearch()
                setUserRightButton()
            Else
                dsLogTran = Session("dsTblMaster")
                gvLogTran.DataSource = dsLogTran.Tables("sLogTran")
                gvLogTran.DataBind()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dsLogTran
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ผู้ดูแลระบบ > ประวัติการใช้งาน > ประวัติการเข้าใช้โปรแกรม"
    End Sub

#End Region
#Region " Even Button "

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        IsDataBind()
        clearGvIndex()
    End Sub

#End Region

#Region "Even GridView"

    Protected Sub gvLogTran_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvLogTran.PageIndexChanging
        gvLogTran.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

    Protected Sub gvLogTran_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvLogTran.Sorting
        Dim dt = TryCast(dsLogTran.Tables("sLogTran"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            gvLogTran.DataSource = dsLogTran.Tables("sLogTran")
            gvLogTran.DataBind()
            clearGvIndex()
        End If
    End Sub

    Protected Sub gvLogLogin_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLogTran.RowDataBound
        Try
            convertDate = New clsDateFormat
            If (e.Row.RowType.ToString = "DataRow") Then
                If Not (e.Row.Cells(4).Text = Nothing) Then
                    Dim dteDateLogin As Date = e.Row.Cells(4).Text
                    Dim strDate As String = ""
                    strDate = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, strDateIsChk:=dteDateLogin.ToShortDateString)
                    strDate = strDate & " " & dteDateLogin.ToLongTimeString
                    e.Row.Cells(4).Text = strDate

                End If

            End If
        Catch ex As Exception
            WriteError(ex, "gvLogLogin_RowDataBound", "frmTran")
        End Try


    End Sub

#End Region
#Region " Function"
    Private Sub setUserRightButton()

        'Dim col As New Collection
        'col.Add(btnSearch, statusbtn.Query.ToString)
        'setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub

    Private Sub LoadDataToDropdown()

        'clsSessionMenuRight = Session("ClsMenuRight")
        'clsMasterDropDown = New clsMasterDropDown

        'clsMasterDropDown.setUserRightToDdlSearch(ddlUserRight, clsSessionMenuRight.getRoleGroup)

        'clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
        '                                 strDeptCode:=clsSessionMenuRight.getDeptCode, _
        '                                 strRoleCode:=clsSessionMenuRight.getRoleGroup)

        'clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, _
        '                                           strBranchCode:=clsSessionMenuRight.getBranchCode, _
        '                                     strRoleCode:=clsSessionMenuRight.getRoleGroup, _
        '                                     strZoneCode:=clsSessionMenuRight.getZoneCode)
        'Dim qryLogTran = From LogTran In LogTranEntity.LOG_TRANSACTION Join program In LogTranEntity.SPROGRAM On LogTran.PROGRAMCODE Equals program.PROGRAMCODE Select LogTran.PROGRAMCODE, program.PROGRAMNAME
        Try
            Dim ProEntity As New UserEntities

            Dim qryProgram = From program In ProEntity.SPROGRAM Where program.PROGRAMCODE <> "M55" And program.PROGRAMCODE <> "M56" Select program.PROGRAMCODE, program.PROGRAMNAME

            Dim AddItem As New ListItem(Resources.resClsMasterDropdown.Select_Search, 0)
            ddlProgram.DataSource = qryProgram.Distinct.OrderBy(Function(fn) fn.PROGRAMCODE).ToList
            ddlProgram.DataTextField = "PROGRAMNAME"
            ddlProgram.DataValueField = "PROGRAMCODE"
            ddlProgram.DataBind()
            ddlProgram.Items.Insert(0, AddItem)
        Catch ex As Exception
            WriteError(ex, "LoadDataToDropdown", "frmTran")

        End Try


    End Sub

    Public Sub setDefaultSearch()
        Try
            clsFormatDate = New clsDateFormat
            txtStartDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
            txtEndDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
            '  rdolType.SelectedIndex = 0
            chkSearch.Checked = True
            chkAdd.Checked = True
            chkEdit.Checked = True
            chkDelete.Checked = True
            chkPrint.Checked = True
        Catch ex As Exception
            WriteError(ex, "setDefaultSearch", "frmTran")
        End Try


    End Sub

    Private Sub clearGvIndex()
        gvLogTran.SelectedIndex = -1
        '  btnDelete.Attributes.Clear()
    End Sub

    Private Sub IsDataBind()
        Try
            Dim clsFormatDate As New clsDateFormat
            clsSessionMenuRight = Session("ClsMenuRight")

            If dsLogTran.Tables.Contains("sLogTran") Then
                dsLogTran.Tables.Clear()
            End If
            'logLoginEntity1

            Dim whereOr As String = " where "
            Dim qtyWhere As String = " "



            Dim qryLogTran = From LogTran In LogTranEntity.LOG_TRANSACTION
                             Order By LogTran.LOGDATE Descending
                              Select LogTran.UNIQUEID, LogTran.UPDATEBY, LogTran.TRANTYPE, tranName = If(LogTran.TRANTYPE = 1, "ค้นหาข้อมูล", If(LogTran.TRANTYPE = 2, "พิมพ์ฟอร์ม", If(LogTran.TRANTYPE = 3, "บันทึึก ข้อมูล", If(LogTran.TRANTYPE = 4, "บันทึก แก้ไข", "ลบข้อมูล")))), LogTran.DESCRIPTION, LogTran.LOGDATE, LogTran.PROGRAMCODE

            If chkSearch.Checked = True Then

                If Not qtyWhere = "" Then
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 1"
                    whereOr = " Or "
                Else
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 1"
                    whereOr = " Or "
                End If

            End If
            If chkAdd.Checked = True Then


                If Not qtyWhere = "" Then
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 3"
                    whereOr = " Or "
                Else
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 3"
                    whereOr = " Or "
                End If

            End If


            If chkEdit.Checked = True Then

                If Not qtyWhere = "" Then
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 4"
                    whereOr = " Or "
                Else
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 4"
                    whereOr = " Or "
                End If

            End If
            If chkDelete.Checked = True Then

                If Not qtyWhere = "" Then
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 5"
                    whereOr = " Or "
                Else
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 5"
                    whereOr = " Or "
                End If

            End If

            If chkPrint.Checked = True Then


                If Not qtyWhere = "" Then
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 2"
                    whereOr = " Or "
                Else
                    qtyWhere = qtyWhere & whereOr & "LogTran.TRANTYPE = 2"
                    whereOr = " Or "
                End If
            End If


            Dim qryLogTran1 = LogTranEntity.CreateQuery(Of LOG_TRANSACTION)("select value LogTran from LOG_TRANSACTION as LogTran " & qtyWhere)

            If Not (ddlProgram.SelectedValue = "0") Then
                qryLogTran1 = qryLogTran1.Where(Function(LogTran) LogTran.PROGRAMCODE = ddlProgram.SelectedValue)
            End If
            If Not (txtUnique.Text = "") Then
                qryLogTran1 = qryLogTran1.Where(Function(LogTran) LogTran.UNIQUEID.Contains(txtUnique.Text))
            End If

            If Not (txtUserName.Text = "") Then
                qryLogTran1 = qryLogTran1.Where(Function(LogTran) LogTran.UPDATEBY.Contains(txtUserName.Text))
            End If

            'Dim iCount As Integer = 0

            'If (chkSearch.Checked) Then
            '    iCount += 1
            'End If

            'If (chkAdd.Checked) Then
            '    iCount += 1
            'End If

            'If (chkEdit.Checked) Then
            '    iCount += 1
            'End If

            'If (chkPrint.Checked) Then
            '    iCount += 1
            'End If

            'If Not (iCount = 0) Then

            '    If (iCount = 1) Then
            '        qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.TRANTYPE = If(chkSearch.Checked, 1, If(chkAdd.Checked, 3, If(chkEdit.Checked, 4, 2))))
            '    ElseIf iCount = 2 Then
            '        qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.TRANTYPE = If(chkSearch.Checked, 1, If(chkAdd.Checked, 3, If(chkEdit.Checked, 4, 2))) Or LogTran.TRANTYPE = If(chkPrint.Checked, 2, If(chkEdit.Checked, 4, If(chkAdd.Checked, 3, 1))))

            '    ElseIf iCount = 3 Then
            '        qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.TRANTYPE = If(chkSearch.Checked, 1, 3) Or LogTran.TRANTYPE = If(chkAdd.Checked And Not chkEdit.Checked, 3, If(Not chkAdd.Checked And chkEdit.Checked, 4, If(chkSearch.Checked And chkAdd.Checked, 3, If(chkEdit.Checked And chkPrint.Checked, 4, 0)))) Or LogTran.TRANTYPE = If(chkPrint.Checked, 2, 4))

            '    ElseIf iCount = 4 Then
            '        qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.TRANTYPE = 1 Or LogTran.TRANTYPE = 2 Or LogTran.TRANTYPE = 3 Or LogTran.TRANTYPE = 4)
            '    End If
            'Else
            '    qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.TRANTYPE = 0 Or LogTran.TRANTYPE = 0 Or LogTran.TRANTYPE = 0 Or LogTran.TRANTYPE = 0)

            'End If



            If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
                Dim strStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text) & " 00:00:00"
                Dim strEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) & " 23:59:59"

                'qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.LOGDATE >= strStartDate And LogTran.LOGDATE <= strEndDate)
                qryLogTran1 = qryLogTran1.Where(Function(LogTran) LogTran.LOGDATE >= strStartDate And LogTran.LOGDATE <= strEndDate)
            End If

            If Not (txtDescription.Text = "") Then
                ' qryLogTran = qryLogTran.Where(Function(LogTran) LogTran.DESCRIPTION.Contains(txtDescription.Text))
                qryLogTran1 = qryLogTran1.Where(Function(LogTran) LogTran.DESCRIPTION.Contains(txtDescription.Text))
            End If




            '--------------เช็ค การติ้ก  chechbox
            '
            'Dim tblsLogTran = From tblLogTran In qryLogTran Join tbluser In LogTranEntity.SUSER On tblLogTran.UPDATEBY Equals tbluser.USERNAME
            '         Select tblLogTran.UNIQUEID, tblLogTran.UPDATEBY, tblLogTran.TRANTYPE, tblLogTran.tranName, _
            '         tblLogTran.DESCRIPTION, tblLogTran.LOGDATE, tblLogTran.PROGRAMCODE, tbluser.BRANCHCODE, tbluser.DEPTCODE, tbluser.ROLECODE, tbluser.ZONECODE


            Dim tblsLogTran = From tblLogTran In qryLogTran1 Join tbluser In LogTranEntity.SUSER On tblLogTran.UPDATEBY Equals tbluser.USERNAME
                                Order By tblLogTran.LOGDATE Descending _
                                Select tblLogTran.UNIQUEID, tblLogTran.UPDATEBY, tblLogTran.TRANTYPE, tranName = If(tblLogTran.TRANTYPE = 1, "ค้นหาข้อมูล", If(tblLogTran.TRANTYPE = 2, "พิมพ์ฟอร์ม", If(tblLogTran.TRANTYPE = 3, "บันทึึก ข้อมูล", If(tblLogTran.TRANTYPE = 4, "บันทึก แก้ไข", "ลบข้อมูล")))), _
                                    tblLogTran.DESCRIPTION, tblLogTran.LOGDATE, tblLogTran.PROGRAMCODE, tbluser.BRANCHCODE, tbluser.DEPTCODE, tbluser.ROLECODE, tbluser.ZONECODE

            If (clsSessionMenuRight.getRoleGroup > 89) Then
                tblsLogTran = tblsLogTran.Where(Function(fn) fn.ROLECODE <= clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
                tblsLogTran = tblsLogTran.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
                tblsLogTran = tblsLogTran.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
                tblsLogTran = tblsLogTran.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup And fn.ZONECODE = clsSessionMenuRight.getZoneCode)
            ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
                tblsLogTran = tblsLogTran.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
                End If


            dsLogTran.Tables.Add(clsDataTable.LINQToDataTable(tblsLogTran, "sLogTran").Copy)
            If (tblsLogTran.Count = 0) Then
                lblTotal.Text = ""
                lblMsg.Visible = True
            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " + tblsLogTran.Count.ToString + " รายการ "
                End If


            gvLogTran.DataSource = dsLogTran.Tables("sLogTran") 'qryLogTran.ToList 
            gvLogTran.DataBind()


            clsDataTable = Nothing
        Catch ex As Exception
            WriteError(ex, "IsDataBind", "frmTran")
            ScriptManager.RegisterStartupScript(Me, _
                                                        Me.GetType(), _
                                                        "clientScript", _
                                                        "alert('" + "เกิดข้อผิดพลาดในการค้นหาข้อมูล" + "')", _
                                                        True)
        End Try


    End Sub



    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

#End Region
End Class
